<template>
  <div id="downloadFileWrap" style="display: none">
    <a id="download_file" download hidden />
  </div>
</template>

<script>
export default {
  name: 'Index',
  created: function() {
    this.$bus.on('downloadExcel', (obj) => {
      if (obj instanceof Object) {
        if (!obj.url) {
          console.error('请传递下载excel的Url')
          return
        }
        const query = obj.query

        let url = process.env.VUE_APP_BASE_API + obj.url + '?'
        const param = []
        for (const key in query) {
          param.push(key + '=' + query[key])
        }
        url += param.join('&')
        document.getElementById('download_file').href = url
        document.getElementById('download_file').click()
      } else {
        console.error('使用下载excel时，传递参数只能是对象')
      }
    })
  }
}
</script>

<style scoped>

</style>
